<?php

class SalesOrderReportForm extends CFormModel {

    public $startDate;
    public $endDate;
    public $status;
    public $state;

    public function rules() {
        return array(
            array('startDate,endDate', 'required'),
            array('startDate,endDate', 'date', 'format' => 'yyyy-MM-dd'),
            array('status,state','safe')
        );
    }

    public function getStatusList() {
        $list=JobsStatus::getListData();
        $list['']='All';
        return $list;
    }

    public function getStateList(){
        $stateList = array(
            NULL=>'N/A'
        );
        $stateList += Locations::model()->getStateList();
        return $stateList;
    }
    public function getData() {
        $invoiceList=Yii::app()->db->createCommand('select salesordersorg from tbl_invoice where invoicetype=:type')
                ->bindValue('type', Invoice::TYPE_INVOICE)->queryAll();
        $excludeList=array();
        foreach($invoiceList as $item){
            $excludeList[]=$item['salesordersorg'];
        }
        
        $crit = new CDbCriteria();
        $crit->alias = 'i';
        $crit->addNotInCondition('i.invoice', $excludeList);
        $crit->addColumnCondition(array(
            'i.invoicetype'=>  Invoice::TYPE_SALES_ORDERS
        ));
        
        $crit->compare('i.salesorderstatus', $this->status,false,'and');
        $crit->addBetweenCondition('i.invoicedate', $this->startDate, $this->endDate);
        
        $crit->join='left join tbl_customers c on c.custno = i.custno';
        $crit->compare('c.state', $this->state);
        //$crit->compare('')
        $crit->order = 'CAST( SUBSTR( i.invoice,1, LOCATE(  "-", i.invoice )-1) AS SIGNED ) DESC,CAST( SUBSTR( i.invoice, LOCATE(  "-", i.invoice ) +1 ) AS SIGNED ) DESC ';
        $invoiceList = Invoice::model()->findAll($crit);
        return $invoiceList;
    }

}

?>
